package com.meixiang.adapter.myFundAdapter;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.fund.FundOptionalBean;
import com.meixiang.entity.fund.FundOptionalResult;
import com.meixiang.recyclerview.ItemTouchHelperAdapter;
import com.meixiang.recyclerview.OnStartDragListener;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.tool.Tool;

import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的基金 ----自选基金
 */
public class FundOptionalAdapter extends RecyclerAdapter<FundOptionalAdapter.MyViewHolder> {

    private Context context;
    private List<FundOptionalBean> fund_select_list;
    private int index = 0 ;

    public FundOptionalAdapter(Context context) {
        this.context = context;
    }

    /**
     * 刷新数据
     *
     * @param fund_select_list
     */
    public void refreshData(List<FundOptionalBean> fund_select_list) {
        this.fund_select_list = fund_select_list;
        notifyDataSetChanged();
    }

    public void refreshIndex(int index){
        this.index = index ;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View layout = LayoutInflater.from(context).inflate(R.layout.list_fund_optional_item, parent, false);
        return new MyViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv_fund_optional_name.setText(fund_select_list.get(position).getFund_name());
        holder.tv_fund_optional_fund_id.setText(fund_select_list.get(position).getFund_id());
        holder.tv_fund_optional_fund_nav.setText(fund_select_list.get(position).getNav());
        if(index == 0){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getDay_rate() + "%");
        }else if(index == 1){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getWeek_rate() + "%");
        }else if(index == 2){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getMon_rate1() + "%");
        }else if(index == 3){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getMon_rate3() + "%");
        }else if(index == 4){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getMon_rate6() + "%");
        }else if(index == 5){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getYear_rate0() + "%");
        }else if(index == 6){
            holder.tv_fund_optional_fund_day_rate.setText(fund_select_list.get(position).getYear_rate1() + "%");
        }
        holder.show_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tool.showTextToast(context, fund_select_list.get(position).getUrl());
            }
        });
    }

    @Override
    public int getItemCount() {
        return fund_select_list == null ? 0 : fund_select_list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.show_image)
        ImageView show_image;
        @Bind(R.id.tv_fund_optional_name)
        TextView tv_fund_optional_name;
        @Bind(R.id.tv_fund_optional_fund_id)
        TextView tv_fund_optional_fund_id;
        @Bind(R.id.tv_fund_optional_fund_nav)
        TextView tv_fund_optional_fund_nav;
        @Bind(R.id.tv_fund_optional_fund_day_rate)
        TextView tv_fund_optional_fund_day_rate;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}